<template>
  <!-- 我的书架 -->
  <div id="main">
    <div class="bookcaselist">
      <h2>用户书架 - 大千世界 - 魑魅魍魉</h2>
      <!-- 用户书架小说数据列表  table -->
      <el-table
        :data="bookselflist"
        border
        stripe
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="书名" prop="book_name" width="150">
          <template slot-scope="scope">
            <div
              @click="JumpDetailRoute(scope.row.book_id)"
              style="cursor:pointer"
            >
              {{ scope.row.book_name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="作者"
          prop="book_author"
          width="100"
        ></el-table-column>
        <el-table-column label="最新章节" prop="book_newest_name" width="300">
          <template slot-scope="scope">
            <div
              @click="
                JumpChapterRoute(scope.row.book_id, scope.row.book_newest_url)
              "
              style="cursor:pointer"
            >
              {{ scope.row.book_newest_name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="书籍情况" prop="book_status" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.book_status === '连载中'"
              >连载中</el-tag
            >
            <el-tag type="warning" v-else>完本</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="更新时间"
          prop="dtime"
          width="180"
        ></el-table-column>
        <el-table-column label="操作" prop="book_status" width="90">
          <template slot-scope="scope">
            <el-button type="warning" size="mini" @click="removeBookId(uid,scope.row.book_id)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pages">
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 20, 45]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uid: '',
      // 请求查询参数对象
      queryInfo: {
        pagenum: 1, // 当前页数
        pagesize: 5 // 每页条数
      },
      // 书架数据列表
      bookselflist: [],
      // 总数据条数
      total: 0
    }
  },
  created() {
    // 获取用户书架列表数据
    this.getBookSelflist()
  },
  methods: {
    // 点击事件，跳转小说详情页
    JumpDetailRoute(book_id) {
      this.$router.push({
        path: '/book/' + book_id
      })
    },
    // 点击事件跳转分类小说
    JumpCatesRoute(url) {
      this.$router.push({
        path: this.$store.state.fiction_cate[url]
      })
    },
    // 点击事件，跳转小说章节页
    JumpChapterRoute(book_id, sort_id) {
      this.$router.push({
        path: '/chapter/' + book_id + '/' + sort_id
      })
    },
    // 获取用户书架列表数据
    async getBookSelflist() {
      const { data: res } = await this.$http.get(`get_bookself_pages`, {
        params: this.queryInfo
      })

      if (res.status !== 200) {
        return this.$message.error('获取用户书架分页数据失败！')
      }

      this.bookselflist = res.data
      this.uid = res.uid
      this.total = res.total
      console.log(res)
    },
    // 监控当前每页条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getBookSelflist()
    },
    // 监控 每页页码值
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getBookSelflist()
    },
    // 点击移除按钮，删除书籍数据
    async removeBookId(user_id,book_id) {
      const confirmResult = await this.$confirm(
        '此操作将永久移除该书籍, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消移除！')
      }

      // 发出删除请求
      const { data: res } = await this.$http.delete(`delete_bookself_infos/${user_id}/${book_id}`)
      // console.log(res)
      if (res.status !== 200) {
        return this.$message.error(res.msg)
      }

      this.$message.success('移除书籍成功!')
      this.getBookSelflist()
    }
  }
}
</script>

<style lang="less" scoped>
#main {
  width: 980px;
  margin: auto;
}
.bookcaselist {
  margin: 10px auto;
  border: 3px solid #a6d3e8;
  width: 974px;
  background: #fef9ef;
}
.bookcaselist h2 {
  margin: 0px;
  background-color: #a6d3e8;
  border-bottom: 1px solid #dddddd;
  font-size: 14px;
  font-weight: bold;
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  padding: 0 0 0 10px;
}
.pages {
  clear: both;
  padding: 2px 0;
  color: #888;
  text-align: center;
}
</style>
